package com.csesteel.crm.service.impl;

import com.csesteel.crm.dao.UserDao;
import com.csesteel.crm.domain.User;
import com.csesteel.crm.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2015/4/21 0021.
 */
@Service
public class UserServiceImpl implements  UserService {

    private Log logger = LogFactory.getLog(UserServiceImpl.class);
    @Autowired
    private UserDao userDao;

    @Autowired
    private MessageSource messageSource;

    @Transactional
    public void authUserByRole(Integer userId, Integer roleId) {
        userDao.authUserByRole(userId,roleId);
    }

    @Transactional
    public User getUserByNameAndPasswd(String name, String passwd) {
        return userDao.getUserByNameAndPassword(name, passwd);
    }

    @Transactional
    public void saveUser(User user) {
        userDao.save(user);

    }

    @Transactional
    public void updateUser(User user) {
        userDao.update(user);
    }

    @Transactional
    public void deleteUser(String id) {
        userDao.deleteUserById(id);
    }

    @Transactional
    public List<User> findAllUserByPage(int pageNo,int pageSize,String name) {
        return userDao.findByPage(pageNo, pageSize, name);
    }

    @Transactional
    public List<User> findAllUserByPageNoParam(int pageNo, int pageSize) {
        return userDao.findByPageNoparams(pageNo, pageSize);
    }

    @Transactional
    public List<User> findAllUser() {
        return userDao.findAll(User.class);
    }

    @Transactional
    public List<User> findAllUserByRoleID(Integer id) {
        return null;
    }

    @Transactional
    public User getUserById(Integer id) {
        return userDao.get(User.class,id);
    }

    @Transactional
    public long findCount() {
        return userDao.findCountUsersNohParams();
    }

    @Transactional
    public long findCountUsersWithParams(String name) {
        return userDao.findCountUsersWithParams(name);
    }

    @Transactional
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();

        User user = this.userDao.getByUsername(username);
            //得到用户的权限
//        auths = this.userDao.loadUserAuthorities( username );
//
//        logger.info("*********************"+username+"***********************");
//        logger.info(user.getAuthorities());
//        logger.info("********************************************************");

        return null;
    }
}
